class LearningProgress {
  final String bookId;
  final int totalWords;
  final int learnedWords;
  final int todayLearned;
  final Map<String, int> dailyRecords; // 格式: "yyyy-MM-dd": count

  LearningProgress({
    required this.bookId,
    required this.totalWords,
    required this.learnedWords,
    required this.todayLearned,
    required this.dailyRecords,
  });

  factory LearningProgress.fromJson(Map<String, dynamic> json) {
    return LearningProgress(
      bookId: json['book_id'],
      totalWords: json['total_words'],
      learnedWords: json['learned_words'],
      todayLearned: json['today_learned'],
      dailyRecords: Map<String, int>.from(json['daily_records']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'book_id': bookId,
      'total_words': totalWords,
      'learned_words': learnedWords,
      'today_learned': todayLearned,
      'daily_records': dailyRecords,
    };
  }

  // 创建一个新的进度对象，更新今日学习数量
  LearningProgress updateTodayLearned(int count) {
    final String today = DateTime.now().toString().split(' ')[0]; // yyyy-MM-dd
    final Map<String, int> updatedRecords = Map.from(dailyRecords);
    updatedRecords[today] = (updatedRecords[today] ?? 0) + count;

    return LearningProgress(
      bookId: bookId,
      totalWords: totalWords,
      learnedWords: learnedWords + count,
      todayLearned: todayLearned + count,
      dailyRecords: updatedRecords,
    );
  }

  // 创建一个新的空进度对象
  factory LearningProgress.empty(String bookId, int totalWords) {
    return LearningProgress(
      bookId: bookId,
      totalWords: totalWords,
      learnedWords: 0,
      todayLearned: 0,
      dailyRecords: {},
    );
  }
} 